﻿Imports System.IO

Public Class Open_Database
    Private Sub RadButton1_Click(sender As Object, e As EventArgs) Handles RadButton1.Click
        Dim ReadINI As FileDialog = New OpenFileDialog

        With ReadINI
            .AddExtension = True
            .Filter = "SmartPlant Manager V4 INI File|smartplantv4.ini"
            .FilterIndex = 1
            '换了一个ini
            If .ShowDialog = DialogResult.OK Then
                DS.SiteNode = .FileName
                'Me.Text = "当前数据库:" + .FileName
                MyPIDConn.ReadUserAndPs_in_PID(.FileName)
                CallResult = MyPIDConn.ConnPID_admin(MyPIDConn.PIDIniDBType, MyPIDConn.PIDSite_Server, MyPIDConn.PIDSite_DBNAME, MyPIDConn.PIDDBSystemUser, MyPIDConn.PIDDBSystemPs)
                If CallResult <> "" Then
                    MessageBox.Show("运行ConnPID_admin时有误。" + vbNewLine + CallResult)
                    Exit Sub
                End If
                '查询所有的plant
                Me.RadListControl1.Items.Clear()

                SelectStr = <string>
select t.name,t.description,t.path,t2.username,t2.password,t2.sp_schema_type,t2.Data_base 
from xxxSchemaName.t_rootitem t 
join xxxSchemaName.t_db_data t2 on t.sp_id=t2.sp_rootitemid
where t.plantgrouptypename='Plant' and t2.sp_schema_type in ('SPAPLANT')
</string>.Value
                dtCreate.Reset()
                MyPIDConn.PIDReadDataByDBA(MyPIDConn.PIDSite_DBNAME, MyPIDConn.PIDSite_User, SelectStr, dtCreate)
                'If MyPIDConn.PIDIniDBType = "ORACLE" Then
                '    Plant_DBNAME = dtCreate(0)("NAME").ToString

                'Else
                '    Plant_DBNAME = dtCreate(0)("Data_base").ToString

                'End If

                For i = 0 To dtCreate.Rows.Count - 1
                    Me.RadListControl1.Items.Add(dtCreate.Rows(i)(0))
                    'Me.ListBox1.Items(i) = dtCreate.Rows(i)(0)
                Next
                RadButton2.Enabled = False

            End If
        End With
    End Sub

    Private Sub RadListControl1_SelectedIndexChanged(sender As Object, e As Telerik.WinControls.UI.Data.PositionChangedEventArgs) Handles RadListControl1.SelectedIndexChanged

        If RadListControl1.SelectedIndex >= 0 Then   '什么都没选,是-1
            RadButton2.Enabled = True
        End If
    End Sub

    Private Sub RadButton2_Click(sender As Object, e As EventArgs) Handles RadButton2.Click

        DS.ProjectNumber = Trim(RadListControl1.SelectedItem.ToString)
        MyPIDConn.PlantName = DS.ProjectNumber
        'RadForm1.Text = "当前数据库: " + DS.ProjectNumber     '当前激活的工厂
        RadForm1.RadLabelElement1.Text = "当前连接的数据库: " + DS.SiteNode     '当前激活的工厂
        SwitchSPEMINI(DS.SiteNode, MyPIDConn.PlantName)
        CallResult = RadForm1.Initial_PIDConn()
        If CallResult <> "" Then
            MessageBox.Show(CallResult)
            Exit Sub
        End If

        LoadDrawing(True)
        Initial_EDE()


        Me.Close()
    End Sub

    Public Sub SwitchSPEMINI(siteIniPath As String, plantName As String)

        Dim iniPath As String = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile) + "\SmartplantManager.INI"
        MyPIDConn.WriteStrINI("SmartPlant Manager", "SiteServer", siteIniPath, iniPath)
        MyPIDConn.WriteStrINI("SmartPlant Manager", "ActivePlant", plantName, iniPath)
        MyPIDConn.WriteStrINI("SmartPlant Manager", "ActiveParentPlant", plantName, iniPath）
        DS = Nothing
        DS = New Llama.LMADataSource


        Dim newProjInfo As String = siteIniPath + "@" + plantName
        Dim reader As TextReader = File.OpenText("RecentlySiteAndPlant.ini")
        Dim line As String = reader.ReadLine() '读第一行
        While line <> ""
            If newProjInfo = line.Substring(InStr(line, "@")) Then
                Exit Sub
            End If
            line = reader.ReadLine() '读下一行
        End While
        reader.Close()
        reader = Nothing
        Using writer As StreamWriter = New StreamWriter(System.Windows.Forms.Application.StartupPath + "\RecentlySiteAndPlant.ini", True)
            writer.WriteLine(Environment.NewLine + plantName + "@" + newProjInfo)
        End Using
    End Sub
End Class
